package com.brkj.course;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import com.brkj.httpInterface.BaseAjaxParams;

import com.brkj.model.DS_StudyTime;
import com.brkj.util.ConstAnts;
import com.brkj.util.JsonUtil;
import com.brkj.util.MyApplication;
import com.brkj.util.TimeHelp;
import com.brkj.httpInterface.HttpInterface.HIF_RecodeStudyTime;
import com.google.gson.Gson;

import android.content.Context;

public class RecordTime {
	
	private Context context;
	private Long startTime;
	private Long endTime;
	private int totalTime; //秒为单位
	private FinalDb db;
	
	public RecordTime(Context context){
		
		this.context = context;
		
		db = FinalDb.create(context, ConstAnts.BRKJ_DB);
	}
	
	public void start(){
		startTime = TimeHelp.geLongTime();
	}
	
	public void end(){
		endTime = TimeHelp.geLongTime();
		
		totalTime = totalTime + (int)((endTime - startTime)/1000);
	}
	
	public void pause(){
		endTime = TimeHelp.geLongTime();
		
		totalTime = totalTime + (int)((endTime - startTime)/1000);
	}
	
	public void restart(){
		startTime = TimeHelp.geLongTime();
	}
	
	public int getTotalTime(){
		return totalTime;
	}
	
	public  void recodeTime(final String courseID, final String time){
		if((courseID == null) || (time == null)){
			return;
		}
		
		DS_StudyTime studyTimes = new DS_StudyTime();
		studyTimes.setCourseID(courseID);
		studyTimes.setStudyTime(time);
		
		List<DS_StudyTime> studyTimeList = new ArrayList<DS_StudyTime>();
		studyTimeList.add(studyTimes);
		Gson gson = new Gson();
		String jsonStr = gson.toJson(studyTimeList);
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_RecodeStudyTime.params.UserID, MyApplication.myUserID);
		params.put(HIF_RecodeStudyTime.params.StudyTimeJson, jsonStr);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_RecodeStudyTime.address, params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t,errorNo, strMsg);
				saveStudyTime(courseID, time);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_RecodeStudyTime.result result = 
						JsonUtil.fromJson((String)t, HIF_RecodeStudyTime.result.class);
				
				if(result == null){
					saveStudyTime(courseID, time);
					return;
				}else{
					if(result.result == 1){
						return;
					}else{
						saveStudyTime(courseID, time);
					}
				} 
			}
			
			
		});
	}
	
	public void uploadLocalData(){
		final List<DS_StudyTime> studyTimesList = 
				db.findAll(DS_StudyTime.class);
		
		Gson gson = new Gson();
		String jsonStr = gson.toJson(studyTimesList);
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_RecodeStudyTime.params.UserID, MyApplication.myUserID);
		params.put(HIF_RecodeStudyTime.params.StudyTimeJson, jsonStr);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_RecodeStudyTime.address, params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t,int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_RecodeStudyTime.result result = 
						JsonUtil.fromJson((String)t, HIF_RecodeStudyTime.result.class);
				
				if(result== null){
					return;
				}
				//上传成功，置0
				if(result.result == 1){
					for(DS_StudyTime studyTime : studyTimesList){
						
						db.deleteByWhere(DS_StudyTime.class, " CourseID='"+studyTime.getCourseID()+ "'");
					}
				}
			}
			
			
		});
	}
	
	
	private void saveStudyTime(String courseID, String time){
		if((courseID == null) || (time == null)){
			return;
		}
		
		DS_StudyTime times = new DS_StudyTime();
		times.setCourseID(courseID);
		
		List<DS_StudyTime> studyTimeList = 
				db.findAllByWhere(DS_StudyTime.class, " CourseID='" + courseID + "'");
		if((studyTimeList!=null) && (studyTimeList.size()>0)){
			
			times.setStudyTime(Integer.toString(Integer.valueOf(time) + Integer.valueOf(studyTimeList.get(0).getStudyTime())));
			db.update(times, " CourseID='" + courseID+ "'");
		}else{
			times.setStudyTime(time);
			db.save(times);
		}
		
	}
}


